package be.ac.ua.comp.projectGPT;
import java.util.Vector;

public class EnglishAuctioneer extends Auctioneer {
	private Vector<EnglishBiddingAgent> mBiddingAgents = new Vector<EnglishBiddingAgent>();
	int mStart;
	int mStep;

	EnglishAuctioneer(Provider provider, int start, int step) {
	    super(provider);
	    mStart = start;
	    mStep = step;
	}
	
	public void addBiddingAgent(EnglishBiddingAgent biddingAgent) {
		mBiddingAgents.add(biddingAgent);
	}
	
	public void removeBiddingAgent(EnglishBiddingAgent biddingAgent) {
		mBiddingAgents.remove(biddingAgent);
	}
	
	protected void finalize() {
		for (EnglishBiddingAgent it : mBiddingAgents) {
			it.withdraw();
		}
	}
	
    public void runAuction() {
        int currentBid = mStart;
        boolean highestBidderChanged;
        EnglishBiddingAgent highestBidder = null;
        do {
            highestBidderChanged = false;
            for (EnglishBiddingAgent it: mBiddingAgents) {
                if (it.bid(currentBid+mStep) && (highestBidder != it)) {
                    if (highestBidder != null) highestBidder.loseBid();
                    highestBidderChanged = true;
                    highestBidder = it;
                    currentBid = currentBid+mStep;
                    System.out.println("agent " + it + " is bidding: " + currentBid);
                }
                
            }
        } while (highestBidderChanged);
    }
}
